﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;
using WalkingTec.Mvvm.Core.Extensions;
using System.Linq;
using System.Collections.Generic;
using nERP.Model.Po;
using nERP.Model.Wms;
using nERP.Model.Bas;
using nERP.ViewModel.Wms.BuyReturnOutWarehouseDetailVMs;
using nERP.Model;

namespace nERP.Wms.Controllers
{
    [AuthorizeJwtWithCookie]
    [ActionDescription("_Model.BuyReturnOutWarehouseDetail")]
    [ApiController]
    [Route("/api/Wms/BuyReturnOutWarehouseDetail")]
    public partial class BuyReturnOutWarehouseDetailController : BaseApiController
    {
        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public IActionResult Get(string id)
        {
            var vm = Wtm.CreateVM<BuyReturnOutWarehouseDetailVM>(id);
            return Ok(vm);
        }

        [ActionDescription("Sys.Create")]
        [HttpPost("[action]")]
        public async Task<IActionResult> Create(BuyReturnOutWarehouseDetailVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();
   
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }

        }

        [ActionDescription("Sys.Edit")]
        [HttpPut("[action]")]
        public async Task<IActionResult> Edit(BuyReturnOutWarehouseDetailVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }
                

        [HttpPost("BatchEdit")]
        [ActionDescription("Sys.BatchEdit")]
        public ActionResult BatchEdit(BuyReturnOutWarehouseDetailBatchVM vm)
        {
            if (!ModelState.IsValid || !vm.DoBatchEdit())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(vm.Ids.Count());
            }
        }

		[HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        public IActionResult BatchDelete(string[] ids)
        {
            var vm = Wtm.CreateVM<BuyReturnOutWarehouseDetailBatchVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = ids;
            }
            else
            {
                return Ok();
            }
            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }

        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<BuyReturnOutWarehouseDetailImportVM>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }
            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }

        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(BuyReturnOutWarehouseDetailImportVM vm)
        {

            if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm.EntityList.Count);
            }
        }



        
        [HttpGet("GetBuyReturns")]
        [AllRights]
        public ActionResult GetBuyReturns()
        {
            return Ok(DC.Set<BuyReturn>().GetSelectListItems(Wtm, x => x.BuyReturnCode, x => x.BuyReturnCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyReturnByBuyReturnId(List<string> id)
        {
            var rv = DC.Set<BuyReturn>().CheckIDs(id).GetSelectListItems(Wtm, x => x.BuyReturnCode, x => x.BuyReturnCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyReturnOutWarehouseDetailByBuyReturn(List<string> id)
        {
            var rv = DC.Set<BuyReturnOutWarehouseDetail>().CheckIDs(id, x => x.BuyReturnId).GetSelectListItems(Wtm,x=>x.BuyReturnOutWarehouseDetailCode, x => x.BuyReturnOutWarehouseDetailCode);
            return Ok(rv);
        }


        [HttpGet("GetBuyReturnOutWarehouses")]
        [AllRights]
        public ActionResult GetBuyReturnOutWarehouses()
        {
            return Ok(DC.Set<BuyReturnOutWarehouse>().GetSelectListItems(Wtm, x => x.BuyReturnOutWarehouseCode, x => x.BuyReturnOutWarehouseCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyReturnOutWarehouseByBuyReturnOutWarehouseId(List<string> id)
        {
            var rv = DC.Set<BuyReturnOutWarehouse>().CheckIDs(id).GetSelectListItems(Wtm, x => x.BuyReturnOutWarehouseCode, x => x.BuyReturnOutWarehouseCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyReturnOutWarehouseDetailByBuyReturnOutWarehouse(List<string> id)
        {
            var rv = DC.Set<BuyReturnOutWarehouseDetail>().CheckIDs(id, x => x.BuyReturnOutWarehouseId).GetSelectListItems(Wtm,x=>x.BuyReturnOutWarehouseDetailCode, x => x.BuyReturnOutWarehouseDetailCode);
            return Ok(rv);
        }


        [HttpGet("GetBuys")]
        [AllRights]
        public ActionResult GetBuys()
        {
            return Ok(DC.Set<Buy>().GetSelectListItems(Wtm, x => x.BuyCode, x => x.BuyCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyByBuyId(List<string> id)
        {
            var rv = DC.Set<Buy>().CheckIDs(id).GetSelectListItems(Wtm, x => x.BuyCode, x => x.BuyCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyReturnOutWarehouseDetailByBuy(List<string> id)
        {
            var rv = DC.Set<BuyReturnOutWarehouseDetail>().CheckIDs(id, x => x.BuyId).GetSelectListItems(Wtm,x=>x.BuyReturnOutWarehouseDetailCode, x => x.BuyReturnOutWarehouseDetailCode);
            return Ok(rv);
        }


        [HttpGet("GetBuyDetails")]
        [AllRights]
        public ActionResult GetBuyDetails()
        {
            return Ok(DC.Set<BuyDetail>().GetSelectListItems(Wtm, x => x.BuyDetailCode, x => x.BuyDetailCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyDetailByBuyDetailId(List<string> id)
        {
            var rv = DC.Set<BuyDetail>().CheckIDs(id).GetSelectListItems(Wtm, x => x.BuyDetailCode, x => x.BuyDetailCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyReturnOutWarehouseDetailByBuyDetail(List<string> id)
        {
            var rv = DC.Set<BuyReturnOutWarehouseDetail>().CheckIDs(id, x => x.BuyDetailId).GetSelectListItems(Wtm,x=>x.BuyReturnOutWarehouseDetailCode, x => x.BuyReturnOutWarehouseDetailCode);
            return Ok(rv);
        }


        [HttpGet("GetWarehouses")]
        [AllRights]
        public ActionResult GetWarehouses()
        {
            return Ok(DC.Set<Warehouse>().GetSelectListItems(Wtm, x => x.WarehouseName, x => x.WarehouseCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetWarehouseByWarehouseId(List<string> id)
        {
            var rv = DC.Set<Warehouse>().CheckIDs(id).GetSelectListItems(Wtm, x => x.WarehouseName, x => x.WarehouseCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyReturnOutWarehouseDetailByWarehouse(List<string> id)
        {
            var rv = DC.Set<BuyReturnOutWarehouseDetail>().CheckIDs(id, x => x.WarehouseId).GetSelectListItems(Wtm,x=>x.BuyReturnOutWarehouseDetailCode, x => x.BuyReturnOutWarehouseDetailCode);
            return Ok(rv);
        }


        [HttpGet("GetGoodss")]
        [AllRights]
        public ActionResult GetGoodss()
        {
            return Ok(DC.Set<Goods>().GetSelectListItems(Wtm, x => x.GoodsName, x => x.GoodsCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetGoodsByGoodsId(List<string> id)
        {
            var rv = DC.Set<Goods>().CheckIDs(id).GetSelectListItems(Wtm, x => x.GoodsName, x => x.GoodsCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetBuyReturnOutWarehouseDetailByGoods(List<string> id)
        {
            var rv = DC.Set<BuyReturnOutWarehouseDetail>().CheckIDs(id, x => x.GoodsId).GetSelectListItems(Wtm,x=>x.BuyReturnOutWarehouseDetailCode, x => x.BuyReturnOutWarehouseDetailCode);
            return Ok(rv);
        }


    }
}